package algorithm_demo.leetcode;

/**
 * 9. 回文数
 * https://leetcode.cn/problems/palindrome-number/
 *
 * @author Api
 * @date 2023/4/24 22:43
 */
public class Problem_009_PalindromeNumber {
    public static boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }

        char[] oldChar = String.valueOf(x).toCharArray();
        int index = oldChar.length - 1;
        char[] newChar = new char[oldChar.length];
        int tmp = 0;
        while (tmp <= oldChar.length - 1) {
            newChar[tmp] = oldChar[index];
            tmp++;
            index--;
        }
        for (int i = 0; i < oldChar.length; i++) {
            if (oldChar[i] != newChar[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        isPalindrome(12341);
    }

    public static boolean isPalindrome1(int x) {
        if (x < 0) {
            return false;
        }
        int cur = 0;
        int tmp = x;
        while (tmp != 0) {
            cur = cur * 10 + tmp % 10;
            tmp /= 10;
        }
        return cur == x;
    }
}